package cn.qzuie.smart.module.dorm.controller.admin.dormitories;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.dormitories.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.dormitories.DormitoriesDO;
import cn.qzuie.smart.module.dorm.service.dormitories.DormitoriesService;

@Tag(name = "管理后台 - 宿舍")
@RestController
@RequestMapping("/dorm/itories")
@Validated
public class DormitoriesController {

    @Resource
    private DormitoriesService itoriesService;

    @PostMapping("/create")
    @Operation(summary = "创建宿舍")
    @PreAuthorize("@ss.hasPermission('dorm:itories:create')")
    public CommonResult<Integer> createitories(@Valid @RequestBody DormitoriesSaveReqVO createReqVO) {
        return success(itoriesService.createitories(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新宿舍")
    @PreAuthorize("@ss.hasPermission('dorm:itories:update')")
    public CommonResult<Boolean> updateitories(@Valid @RequestBody DormitoriesSaveReqVO updateReqVO) {
        itoriesService.updateitories(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除宿舍")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:itories:delete')")
    public CommonResult<Boolean> deleteitories(@RequestParam("id") Long id) {
        itoriesService.deleteitories(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除宿舍")
                @PreAuthorize("@ss.hasPermission('dorm:itories:delete')")
    public CommonResult<Boolean> deleteitoriesList(@RequestParam("ids") List<Long> ids) {
        itoriesService.deleteitoriesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得宿舍")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:itories:query')")
    public CommonResult<DormitoriesRespVO> getitories(@RequestParam("id") Long id) {
        DormitoriesDO itories = itoriesService.getitories(id);
        return success(BeanUtils.toBean(itories, DormitoriesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得宿舍分页")
    @PreAuthorize("@ss.hasPermission('dorm:itories:query')")
    public CommonResult<PageResult<DormitoriesRespVO>> getitoriesPage(@Valid DormitoriesPageReqVO pageReqVO) {
        PageResult<DormitoriesDO> pageResult = itoriesService.getitoriesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DormitoriesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出宿舍 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:itories:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportitoriesExcel(@Valid DormitoriesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DormitoriesDO> list = itoriesService.getitoriesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "宿舍.xls", "数据", DormitoriesRespVO.class,
                        BeanUtils.toBean(list, DormitoriesRespVO.class));
    }

}